package com.tpshion.cloud.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tpshion.cloud.auth.domain.dto.AddMenuDto;
import com.tpshion.cloud.auth.domain.dto.UpdateMenuDto;
import com.tpshion.cloud.auth.domain.entity.Menu;
import com.tpshion.cloud.auth.domain.entity.Outpath;
import com.tpshion.cloud.auth.domain.entity.RoleMenu;
import com.tpshion.cloud.auth.mapper.MenuMapper;
import com.tpshion.cloud.auth.mapper.OutpathMapper;
import com.tpshion.cloud.auth.mapper.RoleMenuMapper;
import com.tpshion.cloud.auth.service.MenuService;
import com.tpshion.cloud.auth.utils.MenuUtil;
import com.tpshion.cloud.auth.utils.support.MenuData;
import com.tpshion.cloud.common.support.Assert;
import com.tpshion.cloud.common.support.ErrorCode;
import com.tpshion.cloud.common.utils.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private OutpathMapper outpathMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public List<MenuData> getAllMenuData(String sysCode) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sys_code", sysCode);
        queryWrapper.orderByAsc("create_time");
        List<Menu> menus = menuMapper.selectList(queryWrapper);
        List<MenuData> allMenuData = MenuUtil.getAllMenuData(menus);
        return allMenuData;
    }

    @Override
    @Transactional
    public Menu updateMenu(UpdateMenuDto updateMenuDto) {
        Menu menu = menuMapper.selectById(updateMenuDto.getId());
        Assert.notNull(menu, ErrorCode.MENU_NOT_EXIST);
        menu.setTitle(updateMenuDto.getTitle());
        menu.setName(updateMenuDto.getName());
        menu.setIcon(updateMenuDto.getIcon());
        menu.setSysCode(updateMenuDto.getSysCode());
        menu.setUpdateTime(LocalDateTime.now());
        menuMapper.updateById(menu);

        if(!StringUtils.isEmpty(updateMenuDto.getOutPath())){
            saveOutPath(updateMenuDto.getName(),updateMenuDto.getOutPath());
        }

        return menu;
    }

    @Override
    @Transactional
    public Menu addMenu(AddMenuDto addMenuDto) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title", addMenuDto.getTitle());
        queryWrapper.eq("name", addMenuDto.getName());
        queryWrapper.eq("sys_code", addMenuDto.getSysCode());
        Assert.isTrue(menuMapper.selectCount(queryWrapper) <= 0, ErrorCode.MENU_IS_EXIST);

        Menu menu = new Menu();
        menu.setId(IdUtil.getUUID());
        menu.setTitle(addMenuDto.getTitle());
        menu.setName(addMenuDto.getName());
        menu.setIcon(addMenuDto.getIcon());
        menu.setSysCode(addMenuDto.getSysCode());
        menu.setParentId(addMenuDto.getParentId());
        menu.setCreateTime(LocalDateTime.now());
        menuMapper.insert(menu);

        if(!StringUtils.isEmpty(addMenuDto.getOutPath())){
            saveOutPath(addMenuDto.getName(),addMenuDto.getOutPath());
        }
        return menu;
    }

    @Override
    @Transactional
    public void deleteMenu(List<String> menuIds) {
        menuMapper.deleteBatchIds(menuIds);
        // 删除菜单角色关系
        menuIds.stream().forEach(item -> {
            QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
            menuQueryWrapper.eq("parent_id", item);
            menuMapper.delete(menuQueryWrapper);

            QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("menu_id", item);
            roleMenuMapper.delete(queryWrapper);
        });
    }

    private void saveOutPath(String name, String outPath){
        // 如果外链不为空，需要保存外链地址
        QueryWrapper<Outpath> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        List<Outpath> list = outpathMapper.selectList(queryWrapper);
        if(list.isEmpty()){
            // 创建新的外链
            Outpath outpath = new Outpath();
            outpath.setName(name);
            outpath.setPath(outPath);
            outpath.setCreateTime(LocalDateTime.now());
            outpathMapper.insert(outpath);
        }else{
            // 修改外链地址
            Outpath outpath = list.get(0);
            outpath.setPath(outPath);
            outpath.setUpdateTime(LocalDateTime.now());
            outpathMapper.updateById(outpath);
        }
    }
}
